package com.itheima.advantagesup.service.impl;

import com.itheima.advantagesup.common.constant.LoginConstant;
import com.itheima.advantagesup.common.constant.MessageConstant;
import com.itheima.advantagesup.entity.YddCate;
import com.itheima.advantagesup.entity.YddUser;
import com.itheima.advantagesup.exception.BusinessException;
import com.itheima.advantagesup.mapper.UserLoginMapper;
import com.itheima.advantagesup.service.UserLoginService;
import com.itheima.advantagesup.utils.JwtUtil;
import com.itheima.advantagesup.vo.IndexVO;
import com.itheima.advantagesup.vo.YddCateVO;
import com.itheima.advantagesup.vo.YddUserLoginVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Objects;

/**
 * @author iTaotao
 * @version 1.0.0
 * @ClassName UserLoginServiceImpl.java
 * @Description 用户登录接口实现类
 * @createTime 2023年07月30日 18:03:00
 */
@Service
public class UserLoginServiceImpl implements UserLoginService {
    @Autowired
    private UserLoginMapper userLoginMapper;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private JwtUtil jwtUtil;
    @Override
    public YddUserLoginVO login(YddUser yddUser) {
      YddUser user =  userLoginMapper.selectUserByUserPhone(yddUser.getMobile());
        if(Objects.isNull(user)){
            throw new BusinessException(MessageConstant.PHONE_ERROR);
        }
        String password = DigestUtils.md5DigestAsHex(yddUser.getPassword().getBytes());
        if(!password.equals(yddUser.getPassword())){
            throw new BusinessException(MessageConstant.PASSWORD_ERROR);
        }
        String token = request.getHeader("token");
        YddUserLoginVO yddUserLoginVO = new YddUserLoginVO();
        yddUserLoginVO.setYddUser(yddUser);
        yddUserLoginVO.setArticleNum(2);
        yddUserLoginVO.setCollectNum(2);
        yddUserLoginVO.setToken(token);
        yddUserLoginVO.setZanNum(99);
        HashMap<String, Object> map = new HashMap<>(8);
        map.put(LoginConstant.USER_ID, yddUser.getId());
        String jwt = jwtUtil.createJWT(map);
        return yddUserLoginVO;


    }

    @Override
    public IndexVO indexUser() {
        IndexVO indexVO = new IndexVO();
      //  YddCateVO yddCateVO = getYddCateVO();
        return null;

    }


}
